package com.bjwl.controller.safty;

import java.util.Iterator;
import java.util.List;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.bjwl.base.Constants;
import com.bjwl.base.CurrUser;
import com.bjwl.base.Result;
import com.bjwl.service.safty.HomeService;
import com.bjwl.view.MenuView;

@RestController
@RequestMapping("/safty/home")
public class HomeController {
	//声明业务层对象
	@Autowired
	private HomeService homeService;
	@GetMapping("/getCurrUser")
	public CurrUser getCurrUser(HttpSession session) {
		CurrUser currUser=(CurrUser)session.getAttribute("SESSION_ATTR_CURRUSER");
		System.out.println("Homesession:"+currUser);
		if(currUser!=null) {
			return currUser;
		}
		return null;	
	}
	@GetMapping("/exit")
	public Result exit(HttpSession session) {
		//移除SessionId
		session.removeAttribute(Constants.SESSION_ATTR_CURRUSER);
		return Result.success("您已退出");		
	}
	@GetMapping("/getMenuList")
	public List<MenuView> getMenuList(String u_id) {
		System.out.println("u_id:"+u_id);
		//调用业务层方法查询菜单列表对象
		List<MenuView> menuList=homeService.getMenuList(u_id);
		
		
		Iterator<MenuView> iter=menuList.iterator();
		while (iter.hasNext()) {
			MenuView mainMenu=iter.next();
			System.out.println("主菜单："+mainMenu.getM_name()+"  "+mainMenu.getM_id()+"  ");
			List<MenuView> sub=mainMenu.getSubMenuViews();
			for(MenuView mView:sub) {
				System.out.println("子菜单："+mView.getM_id()+"   "+mView.getM_name()+"  "+mView.getM_url());
			}
			
		}
		
		return menuList;	
	}

}
